/**
 *	登录后主页控制器
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Controller.api;



import com.framerock.server.Response;
import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.Chajian.Chajianrockfile;
import com.framework.ACore.Model;
import com.framework.Model.ModelCompany;
import com.framework.Model.ModelOption;


public class ControllerHome extends ControllerApi {



    /**
     * 登录页数据
     * */
    public String data()
    {
        //读取菜单
        String where   = "";
        if(mRequest.USERTYPE==0){
            where=" and `ispir`=0";
        }else{
            where=" and `ispir` in(0,1)";
        }
        String rowsstr = Json.getListString(Model.get(this.nDb, "menu").getAll("`status`=1 "+where+"", "*", "`pid` asc, `sort` asc"));
        Json json = new Json();
        json.putVal("menuarr", rowsstr);

        //读取平台个人信息
        json.putVal("userinfo", Rock.getMap(new String[]{
                "id",mRequest.getLoginrs("id"),
                "name",mRequest.getLoginrs("name"),
                "mobile",mRequest.getLoginrs("mobile"),
                "face",mRequest.getLoginrs("face"),
                "type",mRequest.getLoginrs("type"),
        }));

        json.putVal("rockfile", new Chajianrockfile(this.nDb).getInfo());
        json.putVal("systemtitle", new ModelOption(this.nDb).getVal("systemtitle"));



        return Response.returnSuccess(json.getJson());
    }

    protected String runAction(String action)
    {
        if(action.equals("change"))return change();
        return super.runAction(action);
    }




    //首页单位数据
    public String index()
    {
        Json json        = new Json();
        ModelCompany obj = ModelCompany.get(this.nDb);
        json.putVal("companyCreate", Json.getListString(obj.companyCreate(mRequest.USERID)));
        json.putVal("companyJoin", Json.getListString(obj.companyJoin(mRequest.USERID)));

        if(mRequest.USERTYPE>0){
            json.putInt("usershu", Model.get(this.nDb,"users").getCount("1=1"));
            json.putInt("companyshu", Model.get(this.nDb,"company").getCount("1=1"));
        }
        json.putInt("usertype", mRequest.USERTYPE);

        return Response.returnSuccess(json.getJson());
    }

    //切换单位
    private String change()
    {
        String cid = mRequest.get("cid");
        Model.get(this.nDb, "users").update("`dcid`="+cid+"", mRequest.USERID+"");
        return Response.returnSuccess();
    }
}
